<?php
require_once('../phplib/include.inc.php');

class DownLoad{
	static function DoDownLoad(){
		$action = $_REQUEST['request'];
		if($action == 'truck-type'){
			DownLoad::GetTruckData();
		}
		elseif ($action == 'weld-gap'){
			DownLoad::GetWeldGap();
		}
		else {
			die('Please let me know what you want?');
		}
	}
	static function CheckLogin(){
		$user = $_REQUEST["user"];
		$pwd = $_REQUEST["pwd"];
		if($user === null || strlen($user) == 0){
			die("Login error: please post your user id.");
		}
		if($user === null || strlen($user) == 0){
			die('Login error: please post your password.');
		}
//		$db = getDb(true);
//		$result = $db->fetchOne("SELECT * FROM user_t WHERE uid={0}",array($user));
//		if($result == null){
//			die("Login error: no such user $user");
//		}
//		if($result["pwd"] != md5($pwd)){
//			die("Login error: wrong password.");
//		}
		if($user != '1' || $pwd !='123'){
			die('Login error: please check your user name and password.');
		}
	}
	static function GetTruckData(){
		DownLoad::CheckLogin();
		$db = getDb(true);
		$types = $db->fetch("SELECT * FROM type_t");
		$str = '$';
		for($i = 0;$i < count($types); $i++ ){
			$str = $str.'*'.$types[$i]['tname'];
			$cls = $db->fetch("SELECT DISTINCT gjcl FROM product_t WHERE tid=".intval($types[$i]['tid']));
			for($j = 0; $j < count($cls); $j++ ){
				$str = $str.'*'.$cls[$j]['gjcl'];
				$rls = $db->fetch("SELECT DISTINCT cxrl FROM product_t WHERE tid={0} AND gjcl={1}",array($types[$i]['tid'],$cls[$j]['gjcl']));
				for($k = 0; $k < count($rls); $k++ ){
					$str = $str.'*'.$rls[$k]['cxrl'].'立方米';
				}
				$str = $str.'*)';
			}
			$str = $str.'*]';
		}
		$str = $str.'*}*';
		$str = mb_convert_encoding($str,'GBK','UTF-8');
		echo $str;
	}
	static function GetWeldGap(){
		//&request=weld-gap&type=油罐车&material=铝合金&capacity=23立方米
		global $ConvertData;
		$type = $_REQUEST['type'];
		$material = $_REQUEST['material'];
		$capacity = $_REQUEST['capacity'];
		$type = mb_convert_encoding($type,'UTF-8','GBK');
		$material = mb_convert_encoding($material,'UTF-8','GBK');
		$capacity = mb_convert_encoding($capacity,'UTF-8','GBK');
		$capacity = str_replace('立方米','',$capacity);
		$db = getDb(true);
		$sql = "SELECT pt.*,tt.tname FROM product_t AS pt LEFT JOIN type_t AS tt ON pt.tid=tt.tid WHERE tt.tname={0} AND pt.gjcl={1} AND pt.cxrl={2}";
		$result = $db->fetch($sql,array($type,$material,$capacity));
		$str = '$';
		for($i = 0; $i < count($result); $i++ ){
			$a = $result[$i];
			$hfbh = $a['hfbh'];
			$czms = $ConvertData['czms'][$a['czms']];
			$hscl = $a['hscl'];
			$hszj = $a['hszj'];
			$bhqt = $a['bhqt'];
			$sssd = $a['sssd'];
			$dhcd = $a['dhcd'];
			$tlxz = $a['tlxz'];
			$hsxz = $a['hsxz'];
			$sssd = intval(65535.0 / 17.3 * (floatval($sssd) - 1.0));
			$dhcd = intval(65535.0 / 60.0 * (floatval($dhcd) + 30.0));
			$tlxz = intval(255.0 / 10.0 * (floatval($tlxz) + 5.0));
			$hsxz = intval(255.0 / 400.0 * (floatval($hsxz) + 200.0));
			if($sssd < 0) $sssd = 0;
			if($sssd > 65535) $sssd = 65535;
			if($dhcd < 0) $dhcd = 0;
			if($dhcd > 65535) $dhcd = 65535;
			if($tlxz < 0) $tlxz = 0;
			if($tlxz > 255) $tlxz = 255;
			if($hsxz < 0) $hsxz = 0;
			if($hsxz > 255) $hsxz = 255;
			$zjxh = $ConvertData['hscl'][$hscl] * 5 + $ConvertData['hszj'][$hszj];
			$str = $str.'*#'.$hfbh.'*'.$czms.'*'.$zjxh.'*'.$hscl.'*'.$hszj.'*'.$bhqt.'*'.$sssd.'*'.$dhcd.'*'.$tlxz.'*'.$hsxz.'*]';
		}
		$str = $str.'*}*';
		echo $str;
	}
}

DownLoad::DoDownLoad();
?>